section .data
	errno dq 0
	addr dq 0

MAP_FAILED equ -1
MAP_LEN equ 40960
;MAP_LEN equ 0xffffffffffffffff
PROT_READ_WRITE equ 3
MAP_SHARED_ANON equ 33
;MAP_SHARED_ANON equ 0x20

section .text
;extern errno
;extern __errno_location
extern strerror
extern printf
;if use ld
;global _start
;if use gcc
global main
;_start:
main:
	and rsp,~0xffff			;堆栈对齐 equ 0xffffffffffff0000
	
	mov rax,9				;mmap NO
	mov rdi,0				;map address
	mov rsi,MAP_LEN				;map size
	mov rdx,PROT_READ_WRITE
	mov r10,MAP_SHARED_ANON
	mov r8,-1				;忽略fd
	mov r9,0				;offset
	syscall
	cmp rax,0xfffffffffffff001
	jb next
	push rax
	neg rax
	mov [errno],rax
	pop rax
	or rax,-1
next:
	cmp rax,MAP_FAILED
	;cmp rax,0
	;js map_failed
	je map_failed
	
	mov [addr],rax
	mov rsi,[addr]
	mov rdi,msg_success
	call printf
	
	mov rax,11
	mov rdi,addr
	mov rsi,MAP_LEN
	syscall
	jmp exit
	
map_failed:	
	;mov rdi,0xb
	;call __errno_location
	;mov rax,qword [rax]
	;mov rsi,[errno]
	
	mov rdi,[errno]
	call strerror
	mov rsi,rax
	mov rdi,msg_failed
	call printf

exit:
	mov rax,60		;exit NO
	mov rdi,0		;error_code
	syscall
	
	msg_success: db "map successed , addr at %p",0ax,0
	msg_failed: db "map failed ,due to %s",0ax,0
	